package com.guoheng.safe.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoheng.safe.common.base.Result;
import com.guoheng.safe.common.constant.CommonConstants;
import com.guoheng.safe.common.enums.ErrorCodeEnum;
import com.guoheng.safe.sys.dto.IdDTO;
import com.guoheng.safe.sys.dto.basicinfo.ProductionPlaceAddDTO;
import com.guoheng.safe.sys.dto.basicinfo.ProductionPlacePageDTO;
import com.guoheng.safe.sys.dto.basicinfo.ProductionPlaceUpdateDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.guoheng.safe.sys.model.basicinfo.ProductionPlace;
import com.guoheng.safe.sys.mapper.basicinfo.ProductionPlaceMapper;
import com.guoheng.safe.sys.service.ProductionPlaceService;

import java.util.List;
/**
 * 类功能描述:　ProductionPlaceServiceImpl
 *
 * @author Eternal
 * @date 2019-34-24 9:34
 */
@Service
public class ProductionPlaceServiceImpl implements ProductionPlaceService{

    @Resource
    private ProductionPlaceMapper productionPlaceMapper;

    @Override
    public Result addProductionPlace(ProductionPlaceAddDTO productionPlaceAddDTO) {
        ProductionPlace productionPlace = new ProductionPlace();
        BeanUtils.copyProperties(productionPlaceAddDTO,productionPlace);
        int result = productionPlaceMapper.insert(productionPlace);
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_CREATE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result updateProductionPlace(ProductionPlaceUpdateDTO productionPlaceUpdateDTO) {
        ProductionPlace productionPlace = new ProductionPlace();
        BeanUtils.copyProperties(productionPlaceUpdateDTO,productionPlace);
        int result = productionPlaceMapper.updateByPrimaryKeySelective(productionPlace);
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_UPDATE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result deleteProductionPlace(IdDTO idDTO) {
        int result = productionPlaceMapper.deleteByPrimaryKey(idDTO.getId());
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_DELETE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result getProductionPlace(IdDTO idDTO) {
        ProductionPlace productionPlace = productionPlaceMapper.selectByPrimaryKey(idDTO.getId());
        if (null == productionPlace){
            return Result.failure(ErrorCodeEnum.SYS_ERR_SEARCH_FAILED);
        }
        return Result.success(productionPlace);
    }

    @Override
    public Result getProductionPlacePage(ProductionPlacePageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum() , pageDTO.getPageSize());
        List<ProductionPlace> productionPlaces = productionPlaceMapper.selectAll();
        PageInfo<ProductionPlace> pageInfo = new PageInfo<>(productionPlaces);
        return Result.success(pageInfo);
    }
}
